package com.shanwen.shopping.http;

import android.app.Activity;
import android.text.TextUtils;

import com.classic.okhttp.base.OkHttpUtils;
import com.classic.okhttp.base.callback.Callback;
import com.shanwen.shopping.http.utils.Base64;

import java.util.HashMap;
import java.util.Map;

/**
 * 使用步骤： <br />
 * 1.在application初始化、并设置企业id: <br />
 * ActionHelp.init(imei,psdnIp, true); <br />
 * ActionHelp.setEnterpriseId(enterpriseId); <br />
 * 2.登录成功后设置key、account:(可能有多个页面有登录逻辑，都要进行设置) <br />
 * ActionHelp.setLoginKey(key); <br />
 * ActionHelp.setAccount(account); <br />
 */
public class ActionHelp {

//	private static String          ENTERPRISE_ID      = "10000000";
//	public  static final String    ACCOUNT            = "18768198003";

    private static String ENTERPRISE_ID = "10000164";
    public static final String ACCOUNT = "18700000001";

//    	private static final String    URL_R              = "http://42.121.255.69:8080/hzlqswserver/"; //商用
    private static final String URL_R = "http://121.41.18.34:600/prisonserver/"; //商用
    //宁波
//    	private static final String    URL_R                = "http://10.119.47.121:8080/prisonserver/"; //商用
    //金华
//    	private static final String    URL_R              = "http://182.169.254.50:8080/prisonserver/"; //商用
    private static final String URL_T = "http://122.224.218.55:555/hzlqswserver/"; //测试
    private static String LOGIN_KEY = "$%!@#123";
    private static final String CHARTSET = "UTF-8";
    private static String ACCOUNTTYPE = "1"; //1手机账号  2 邮箱
    private static String CLIENTTYPE = "android_1.0.0";
    private static String CLIENT_IMEI;
    private static String URL;
    private static boolean ISRELEASE;

    /**
     * 接口调用前，需要调用此方法
     *
     * @param imei
     * @param isRelease 配置服务环境 true:商用，false:商测
     */
    public static void init(String imei, boolean isRelease) {
        CLIENT_IMEI = TextUtils.isEmpty(imei) ? "" : imei;
        ISRELEASE = isRelease;
        URL = isRelease ? URL_R : URL_T;
    }

    /**
     * 获取企业id
     */
    public static String getEnterpriseId() {
        return ENTERPRISE_ID;
    }

    /**
     * 获取帐号类型 1：手机账号, 2: 邮箱
     */
    public static String getAccountType() {
        return ACCOUNTTYPE;
    }

    /**
     * 获取客户端类型
     */
    public static String getClientType() {
        return CLIENTTYPE;
    }

    /**
     * 获取当前帐号
     */
    public static String getAccount() {
        return ACCOUNT;
    }

    /**
     * 获取接口url
     *
     * @return
     */
    public static String getUrl() {
        return URL;
    }

    /**
     * 获取登录key
     */
    public static String getLoginKey() {
        return LOGIN_KEY;
    }

    /**
     * 查询专区模块列表
     *
     * @param activity
     * @param length   <选填>模块列表的最多个数
     *                 <pre>
     *                               如果该值为空或者为0，服务端则根据客户端类型返回默认个数，
     *                               手机最多6个，pad待定；
     *                               如果该值大于0，则返回最多该值的模块个数
     *                               </pre>
     * @param callback
     * @param type     类型，1是代表app，3是大的一体机
     */
    public static void qryModuleList(Activity activity, String type, Integer length, Callback callback) {
        final String timeStamp = String.valueOf(System.currentTimeMillis());
        final Map<String, String> params = getRequestParams(timeStamp);
        if (null != length) params.put("length", String.valueOf(length));
        if (null != type) params.put("type", type);
        params.put("cid", cid3(timeStamp));
        OkHttpUtils.post()
                .url(getUrl("qryModuleList.do"))
                .tag(activity)
                .headers(getHeaders())
                .params(params)
                .build()
                .execute(callback);
    }

    /**
     * 根据模块id查询模块内容列表
     *
     * @param activity
     * @param moduleId           模块id
     * @param currentPublishTime 当前页的最后一条记录的模块内容的发布时间戳
     *                           <pre>如果是重新刷新则为0，否则获取更多的时候就填写当前页最后一条记录的模块内容的发布时间戳</pre>
     * @param length             返回查询记录数,大于0
     * @param callback
     */
    public static void qryModuleContentList(Activity activity, String type, long moduleId, long currentPublishTime,
                                            int length, Callback callback) {
        final String timeStamp = String.valueOf(System.currentTimeMillis());
        final Map<String, String> params = getRequestParams(timeStamp);
        params.put("moduleId", String.valueOf(moduleId));
        params.put("currentPublishTime", String.valueOf(currentPublishTime));
        params.put("length", String.valueOf(length));
        params.put("type", type);
        params.put("cid", cid3(timeStamp + ENTERPRISE_ID + moduleId + currentPublishTime + length));

        OkHttpUtils.post()
                .url(getUrl("qryModuleContentList.do"))
                .tag(activity)
                .headers(getHeaders())
                .params(params)
                .build()
                .execute(callback);
    }


    /**
     * 首页商品banner位查询
     *
     * @param activity
     * @param position banner位置
     *                 <pre>1==电商模块；2==其他待定</pre>
     * @param limit    <可选>banner显示记录数
     *                 <pre>默认为0，如果为0，服务端默认返回4条记录；反之按照参数值返回记录条数</pre>
     * @param callback
     */
    public static void qryB2CBanners(Activity activity, int position, Integer limit, Callback callback) {
        final String timeStamp = String.valueOf(System.currentTimeMillis());
        final Map<String, String> params = getRequestParams(timeStamp);
        params.put("position", String.valueOf(position));
        if (null != limit) params.put("limit", String.valueOf(limit));
        params.put("cid", cid3(timeStamp + ENTERPRISE_ID + position));

        OkHttpUtils.post()
                .url(getUrl("qryB2CBanners.do"))
                .tag(activity)
                .headers(getHeaders())
                .params(params)
                .build()
                .execute(callback);
    }

    /**
     * 查询企业专区banner
     *
     * @param activity
     * @param type     大厅一体机type=3 家属app type=1
     * @param callback
     */
    public static void qryEnterpriseZoneBanner(Activity activity, String type, Callback callback) {
        final String timeStamp = String.valueOf(System.currentTimeMillis());
        final Map<String, String> params = getRequestParams(timeStamp);
        params.put("type", type);
        params.put("cid", cid3(timeStamp + ENTERPRISE_ID));

        OkHttpUtils.post()
                .url(getUrl("qryEnterpriseZoneBanner.do"))
                .tag(activity)
                .headers(getHeaders())
                .params(params)
                .build()
                .execute(callback);
    }

    /**
     * 查询热门活动
     * @param activity
     * @param limit <可选>banner显示记录数
     *              <pre>默认为0，如果为0，服务端默认返回4条记录；反之按照参数值返回记录条数</pre>
     * @param callback
     */
//	public static void qryEPGoodsForHot(Activity activity,Integer limit,Callback callback) {
//		final String timeStamp = String.valueOf(System.currentTimeMillis());
//		final  Map<String,String> params = getRequestParams(timeStamp);
//		if(null!=limit) params.put("limit", String.valueOf(limit));
//		params.put("cid", cid3(timeStamp + ENTERPRISE_ID));
//
//		OkHttpUtils.post()
//				.url(getUrl("qryEPGoodsForHot.do"))
//				.tag(activity)
//				.headers(getHeaders())
//				.params(params)
//				.build()
//				.execute(callback);
//	}

    /**
     * 查询大家都在买
     * <pre>此接口分页暂不采用增量分页方式。</pre>
     *
     * @param activity
     * @param page     当前页,>=1
     * @param pageNum  每页显示记录条数,>0
     * @param callback
     */
    public static void qrySellBestGoods(Activity activity, int page, int pageNum, Callback callback) {
        final String timeStamp = String.valueOf(System.currentTimeMillis());
        final Map<String, String> params = getRequestParams(timeStamp);
        params.put("page", String.valueOf(page));
        params.put("pageNum", String.valueOf(pageNum));
        params.put("cid", cid3(timeStamp + ENTERPRISE_ID + page + pageNum));

        OkHttpUtils.post()
                .url(getUrl("qrySellBestGoods.do"))
                .tag(activity)
                .headers(getHeaders())
                .params(params)
                .build()
                .execute(callback);
    }

    /**
     * 查询分类名称列表信息
     *
     * @param activity
     * @param callback
     */
    public static void qryGoodsItems(Activity activity, Callback callback) {
        final String timeStamp = String.valueOf(System.currentTimeMillis());
        final Map<String, String> params = getRequestParams(timeStamp);
        params.put("cid", cid3(timeStamp + ENTERPRISE_ID));

        OkHttpUtils.post()
                .url(getUrl("qryGoodsItems.do"))
                .tag(activity)
                .headers(getHeaders())
                .params(params)
                .build()
                .execute(callback);
    }

    /**
     * 根据分类id查询该分类下的所有商品列表信息
     *
     * @param activity
     * @param itemId   分类主键编号
     * @param page     当前页,>=1
     * @param pageNum  每页显示记录条数,>0
     * @param callback
     */
    public static void qryGoodsByItemId(Activity activity, String itemId, Callback callback) {
        final String timeStamp = String.valueOf(System.currentTimeMillis());
        final Map<String, String> params = getRequestParams(timeStamp);
        params.put("itemId", String.valueOf(itemId));
//        params.put("page", String.valueOf(page));
//        params.put("pageNum", String.valueOf(pageNum));
        params.put("cid", cid3(timeStamp + ENTERPRISE_ID + itemId ));

        OkHttpUtils.post()
                .url(getUrl("qryGoodsByItemId.do"))
                .tag(activity)
                .headers(getHeaders())
                .params(params)
                .build()
                .execute(callback);
    }

    /**
     * 查询商品列表
     *
     * @param activity
     * @param page      当前页,>=1
     * @param pageNum   每页显示记录条数,>0
     * @param goodsName <可选>商品名称
     * @param callback
     */
    public static void qryAllEpGoods(Activity activity, int page, int pageNum, String goodsName, Callback callback) {
        final String timeStamp = String.valueOf(System.currentTimeMillis());
        final Map<String, String> params = getRequestParams(timeStamp);
        params.put("page", String.valueOf(page));
        params.put("pageNum", String.valueOf(pageNum));
        if (!TextUtils.isEmpty(goodsName)) params.put("goodsName", goodsName);
        params.put("cid", cid3(timeStamp + ENTERPRISE_ID + page + pageNum));

        OkHttpUtils.post()
                .url(getUrl("qryAllEpGoods.do"))
                .tag(activity)
                .headers(getHeaders())
                .params(params)
                .build()
                .execute(callback);
    }

    /**
     * 查询商品基本信息
     *
     * @param activity
     * @param goodsId  商品主键id
     * @param callback
     */
    public static void qryGoodsBase(Activity activity, long goodsId, Callback callback) {
        final String timeStamp = String.valueOf(System.currentTimeMillis());
        final Map<String, String> params = getRequestParams(timeStamp);
        params.put("goodsId", String.valueOf(goodsId));
        params.put("cid", cid3(timeStamp + ENTERPRISE_ID + goodsId));

        OkHttpUtils.post()
                .url(getUrl("qryGoodsBase.do"))
                .tag(activity)
                .headers(getHeaders())
                .params(params)
                .build()
                .execute(callback);
    }

    /**
     * 订单确认
     *
     * @param activity
     * @param json     json格式后的订单对象数据信息
     * @param callback
     */
    public static void confirmOrder(Activity activity, String json, Callback callback) {
        final String timeStamp = String.valueOf(System.currentTimeMillis());
        final Map<String, String> params = getRequestParams(timeStamp);
        params.put("json", json);
        params.put("cid", cid3(timeStamp + ENTERPRISE_ID));

        OkHttpUtils.post()
                .url(getUrl("confirmOrder.do"))
                .tag(activity)
                .headers(getHeaders())
                .params(params)
                .build()
                .execute(callback);
    }

    /**
     * 版本升级
     *
     * @param activity
     * @param versionCode
     * @param callback
     */
    public static void qryNewVersion(Activity activity, String versionCode, Callback callback) {
        final String timeStamp = String.valueOf(System.currentTimeMillis());
        final Map<String, String> params = getRequestParams(timeStamp);
        params.put("versionCode", versionCode);
        params.put("cid", cid3(timeStamp + ENTERPRISE_ID));

        OkHttpUtils.post()
                .url(getUrl("qryNewVersion.do"))
                .tag(activity)
                .headers(getHeaders())
                .params(params)
                .build()
                .execute(callback);
    }


    private static String getUrl(String action) {
        return new StringBuffer(URL).append(action).toString();
    }

    private static String cid(String value) {
        return Base64.base64Encode(value, LOGIN_KEY, CHARTSET);
    }

    private static String cid3(String value) {
        final StringBuffer sb = new StringBuffer(ACCOUNT).append(ACCOUNTTYPE).append(CLIENTTYPE);
        return cid(sb.append(value).toString());
    }

    private static Map<String, String> getRequestParams(String timeStamp) {
        final HashMap<String, String> paramsMap = new HashMap<>();
        paramsMap.put("accountType", ACCOUNTTYPE);
        paramsMap.put("clientType", CLIENTTYPE);
        paramsMap.put("enterpriseId", ENTERPRISE_ID);
        paramsMap.put("account", ACCOUNT);
        paramsMap.put("timeStamp", String.valueOf(System.currentTimeMillis()));
        return paramsMap;
    }

    private static Map<String, String> getHeaders() {
        final HashMap<String, String> headerMap = new HashMap<>();
        headerMap.put("Connection", "Keep-Alive");
        headerMap.put("Chartset", CHARTSET);
        headerMap.put("Cache-Control", "no-cache");
        headerMap.put("Accept-Language", "zh-CN,en-US");
        headerMap.put("client-imei", CLIENT_IMEI);
        headerMap.put("user-ip", "");
        headerMap.put("phone-number", ACCOUNT);
        return headerMap;
    }
}
